<?php
class Invoice extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->helper(array('file','url','date','form'));
		$this->load->library('form_validation');

	}
	
	function index()
	{	
		//Kontrollerar att användaren är inloggad som admin annars 404 fel.
		if ($this->session->userdata('admin') == TRUE)
		{
			//Fält för validering av den postade datan.	
			$this->form_validation->set_rules('startDate','Startdatum','required|valid_date');
			$this->form_validation->set_rules('endDate','Slutdatum','required|valid_date');
	
			if($this->form_validation->run())
			{
				//Det finns något att validera och det är godkännt.
				$startDate=$this->input->post('startDate');
				$endDate=$this->input->post('endDate');
				
				$startDate.=' 00:00:00';
				$endDate.=' 23:59:59';
				
				if($result=$this->_generateInvoiceFile($startDate,$endDate))
				{
					$toView['affectedRows']=$result;
					$this->load->view('admin/view_generate_invoice',$toView);
				}
				else
				{
					//kod om det blir fel vid skapandet av xmlfilen.
					$toView['error']='Det upstod ett problem vid exporteringen.';
					$this->load->view('admin/view_generate_invoice',$toView);
					
				}
				
			}
			else
			{
				// det finns inget att validera.
				$toView['error']='Det upstod ett problem vid exporteringen.';
				$this->load->view('admin/view_generate_invoice',$toView);	
			}
		}
		else
		{
			show_404('page','true');
		}
		
	}
	
	private function _generateInvoiceFile($sDate,$eDate)
	{
		$startStartDate=$sDate;
		$startEndDate=$eDate;
		
		$this->load->model('Invoice_model');
		$result=$this->Invoice_model->GetInvoiceBetweenStartDates($startStartDate,$startEndDate);
		
		if($result)
		{
			$NumberRows=$result->num_rows();
			$doc= new DOMDocument('1.0');
			$rootElement=$doc->createElement('root');
			$doc->appendchild($rootElement);
			
			$invoices=$doc->createElement('invoices');
			$rootElement->appendchild($invoices);
			
			foreach($result->result_object() as $row)
			{	
				//ocrNummer
				$invoice=$doc->createElement('invoice');
				$ocrNumber=$doc->createElement('ocrNumber');
				$ocrNumber->appendchild($doc->createTextNode($row->ocrNummer));
				$invoice->appendchild($ocrNumber);
				//startdatum
				$startDate=$doc->createElement('startDate');
				$startDate->appendchild($doc->createTextNode($row->StartTid));
				$invoice->appendchild($startDate);
				//slutdatum
				$endDate=$doc->createElement('endDate');
				$endDate->appendchild($doc->createTextNode($row->SlutTid));
				$invoice->appendchild($endDate);
				//förfallo datum
				$dueDate=$doc->createElement('dueDate');
				$dueDate->appendchild($doc->createTextNode($row->ForFalloDatum));
				$invoice->appendchild($dueDate);
				//Totalt pris
				$totalPrice=$doc->createElement('totalPrice');
				$totalPrice->appendchild($doc->createTextNode($row->totalPrice));
				$invoice->appendchild($totalPrice);
				//Bil namn
				$carModel=$doc->createElement('carModel');
				$carModel->appendchild($doc->createTextNode($row->Modell));
				$invoice->appendchild($carModel);
				//registreringanummer
				$registrationNumber=$doc->createElement('registrationNumber');
				$registrationNumber->appendchild($doc->createTextNode($row->RegPlåt));
				$invoice->appendchild($registrationNumber);
				//personnummer
				$dateOfBirth=$doc->createElement('dateOfBirth');
				$dateOfBirth->appendchild($doc->createTextNode($row->Personnummer));
				$invoice->appendchild($dateOfBirth);
				//förnamn
				$firstName=$doc->createElement('firstName');
				$firstName->appendchild($doc->createTextNode($row->Fnamn));
				$invoice->appendchild($firstName);
				//efternamn
				$lastName=$doc->createElement('lastName');
				$lastName->appendchild($doc->createTextNode($row->Enamn));
				$invoice->appendchild($lastName);
				
				
				$invoices->appendchild($invoice);
			}
			
			$tempStartDate=date('Ymd',strtotime($startStartDate));
			$tempEndDate=date('Ymd',strtotime($startEndDate));	
			$tempFileName=$tempStartDate.'-'.$tempEndDate;
			
		
			if(write_file('C:/Server/wamp/www/studentbilar/invoices/'.$tempFileName.'.xml',$doc->saveXML(),'w'))
			return $NumberRows;
			else
			echo 'det gick inte att spara ner filen';	
		}
		else
		{
			//returnera falskt
			return FALSE;
		}
		
	}
}		